﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ForumServer;
using ForumServer.Forum;
using log4net;
using System.Collections;
using ForumLibrary;
using ForumLibrary.Data;

namespace ForumGUI
{
    public partial class AddNewFriendFrame : Form
    {
        private ForumManager _manager;
        private ClientController _clientController;
        private string _myUserString;
        private ILog _logger;

        public AddNewFriendFrame(ref ForumManager manager, ref ClientController clientController, string myUserString, ref ILog Ilogger)
        {
            _manager = manager;
            _clientController = clientController;
            _myUserString = myUserString;
            _logger = Ilogger;
            InitializeComponent();
            refreshUserList("");
        }

        private void refreshUserList(string filter)
        {
            userslistBox.Items.Clear();
            string toParse = "";
            try
            {
                toParse = (string)_manager.getUsers();
                _logger.Info(_myUserString + " got users list");
            }
            catch
            {
                _logger.Error(_myUserString + " failed to got users list");
                MessageBox.Show("Server is down. Please Logout and try again in a few minutes");
            }
            String[] parse = toParse.Split('\n');
            foreach (String s in parse)
            {
                if (!String.IsNullOrEmpty(s) && !s.ToLower().Equals(_myUserString.ToLower()) && isNotFriendOfMy(s) && s.ToLower().Contains(filter.ToLower()))
                    userslistBox.Items.Add(s);
            }
        }

        private bool isNotFriendOfMy(string s1)
        {
            List<string> friendsList = new List<string>();
            string toParse = "";
            try
            {
                toParse = (string)_manager.getFriends(_myUserString);
                if (toParse != null)
                    _logger.Info(_myUserString + " got it's friends list");
                else
                    _logger.Error(_myUserString + " failed to got it's friends list");
            }
            catch
            {
                _logger.Error(_myUserString + " failed to got it's friends list");
                MessageBox.Show("Server is down. Please Logout and try again in a few minutes");
            }

            if (!toParse.Contains("User has no friends!"))
            {
                String[] parse = toParse.Split('\n');
                foreach (String s in parse)
                {
                    if (!String.IsNullOrEmpty(s))
                        friendsList.Add(s);
                }
            }

            foreach (string s2 in friendsList)
            {
                if (s1.ToLower().Equals(s2.ToLower()))
                    return false;
            }
            return true;
        }

        private void addFriendButton_Click(object sender, EventArgs e)
        {

            string currSelected = (string)userslistBox.SelectedItem;
            if (currSelected == null)
                return;
            try
            {
                if (_manager.addFriend(_myUserString, currSelected).Contains("Friend added successfully"))
                {
                    _logger.Info(_myUserString + " added " + currSelected + " to his friends");
                    refreshUserList("");
                    MessageBox.Show(currSelected + " added successfully");
                }
                else
                    _logger.Error(_myUserString + " was unable to added " + currSelected + " to his friends");
            }
            catch
            {
                _logger.Error(_myUserString + " failed to added " + currSelected + "to his friends");
                MessageBox.Show("Server is down. Please Logout and try again in a few minutes");
            }
        }

        private void backButton_Click(object sender, EventArgs e)
        {
            Form lastForm = _clientController.popForm();
            if (lastForm is ProfileFrame)
            {
                ProfileFrame pf = (ProfileFrame)lastForm;
                pf.refreshFriendsList();
                pf.refresNotification();
                pf.Visible = true;
                this.Dispose(true);
            }
        }

        private void NewFriendTextBook_TextChanged(object sender, EventArgs e)
        {
            userslistBox.Items.Clear();
            string toParse = "";
            try
            {
                toParse = (string)_manager.searchForNewFriend(_myUserString, this.NewFriendTextBook.Text);
                _logger.Info(_myUserString + " search for friend");
            }
            catch
            {
                _logger.Error(_myUserString + " failed to search for friend");
                MessageBox.Show("Server is down. Please Logout and try again in a few minutes");
            }
            if (toParse.Contains("Fields cannot contain the special character ~"))
            {
                _logger.Error(_myUserString + " failed to search for friend");
                MessageBox.Show("problem at the server side");
                return;
            }

            String[] parse = toParse.Split('\n');
            foreach (String s in parse)
            {
                if (!String.IsNullOrEmpty(s) && !s.Equals(_myUserString) && isNotFriendOfMy(s))
                    userslistBox.Items.Add(s);
            }
        }        


        private void userslistBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.addFriendButton_Click(null, null);
        }
    }
}
